#include "AQ_HexCode.h"


static const unsigned char HEX_DIGITS[] = "0123456789abcdef";
 
char* AQ_HexEncode(char* szDst, const unsigned char* pucSrc, size_t nSrcLen) {
	unsigned char  ch;
	char* szCur = szDst;

	while(nSrcLen--) {
		ch = *pucSrc++;
		*szCur++ = HEX_DIGITS[(ch >> 4) & 0xf];
		*szCur++ = HEX_DIGITS[ch & 0xf];
	}

	*szCur = 0x0;

	return szDst;
}

int AQ_HexDecode(unsigned char* pucDst, size_t nDstLen, const char* szSrc) {
	int iCount = 0;
	size_t iSrcLen = strlen(szSrc);
	if((iSrcLen % 2==1) || (iSrcLen/2 > nDstLen))
		return -1;

	for(; iCount<iSrcLen/2; iCount++) {
		pucDst[iCount] = szSrc[iCount*2]<<4 | szSrc[iCount*2+1];
	}

	return iCount;
}
